/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.ComponentModel;

namespace DaveSexton.DocProject.Sandcastle.TopicManagement
{
	[Serializable]
	[DisplayName("Regular Expression Filter"), Description("Includes or excludes topics with titles that match a regular expresion.")]
	public sealed class RegexTopicFilter : TopicFilter
	{
		#region Public Properties
		public override string DisplayValue
		{
			get
			{
				return string.Format(System.Globalization.CultureInfo.CurrentCulture, 
					Resources.Text.RegexTopicFilterDisplayValue, Options, Environment.NewLine, Pattern);
			}
		}

		public override Type EditorType
		{
			get
			{
				return typeof(RegexTopicFilterControl);
			}
		}

		public string Pattern
		{
			get
			{
				return Settings.Read<string>("pattern", null);
			}
			set
			{
				if (!string.Equals(value, Pattern, StringComparison.Ordinal))
				{
					regex = null;

					if (value == null)
						Settings.Remove("pattern");
					else
						Settings.Write("pattern", value);

					NotifyPropertyChanged("Pattern");
				}
			}
		}

		public RegexOptions Options
		{
			get
			{
				return Settings.Read("options", RegexOptions.None);
			}
			set
			{
				if (value != Options)
				{
					regex = null;

					Settings.Write("options", value);

					NotifyPropertyChanged("Options");
				}
			}
		}

		public Regex Regex
		{
			get
			{
				if (regex == null)
					regex = new Regex(Pattern, Options);

				return regex;
			}
		}
		#endregion

		#region Private / Protected
		private Regex regex;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="RegexTopicFilter" /> class.
		/// </summary>
		public RegexTopicFilter()
		{
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="RegexTopicFilter" /> class.
		/// </summary>
		public RegexTopicFilter(string pattern, RegexOptions options)
		{
			Pattern = pattern;
			Options = options;
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="RegexTopicFilter" /> class.
		/// </summary>
		public RegexTopicFilter(RegexTopicFilter clone)
			: base(clone)
		{
			if (clone == null)
				throw new ArgumentNullException("clone");

			Pattern = clone.Pattern;
			Options = clone.Options;
		}
		#endregion

		#region Methods
		public override bool IsMatch(Topic topic)
		{
			return topic != null && this.Regex.IsMatch(topic.TocTitle);
		}

		public override bool IsMatch(ReflectionApiElement element)
		{
			return element != null && this.Regex.IsMatch(element.Title);
		}

		public override ITopicFilter Clone()
		{
			return new RegexTopicFilter(this);
		}
		#endregion
	}
}
